package com.xdog.web3.controller.token;

import cn.dev33.satoken.annotation.SaIgnore;
import com.alibaba.fastjson.JSONObject;
import com.xdog.common.core.domain.R;
import com.xdog.web3.domain.token.*;
import com.xdog.web3.exception.Web3ApiException;
import com.xdog.web3.service.IWeb3TokenService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Web3代币数据控制器
 */
@Tag(name = "Web3代币数据", description = "Web3代币相关接口")
@RestController
@RequestMapping("/web3/market/token")
public class TokenController {

    @Resource
    private IWeb3TokenService web3TokenService;

    /**
     * 代币搜索
     */
    @Operation(summary = "代币搜索", description = "通过代币名称、代币符号或代币合约地址搜索代币")
    @GetMapping("/search")
    @SaIgnore
    public R<JSONObject> searchTokens(TokenSearchQuery query) {
        try {
            JSONObject result = web3TokenService.searchTokens(query);
            return R.ok(result);
        } catch (Web3ApiException e) {
            return R.fail(e.getHttpStatus(), "API错误: " + e.getMessage());
        } catch (Exception e) {
            return R.fail("代币搜索失败: " + e.getMessage());
        }
    }

    /**
     * 获取代币基础信息
     */
    @Operation(summary = "获取代币基础信息", description = "获取指定代币合约地址的基础信息")
    @PostMapping("/basic-info")
    @SaIgnore
    public R<JSONObject> getTokenBasicInfo(@RequestBody List<TokenBasicInfoRequest> requests) {
        try {
            JSONObject result = web3TokenService.getTokenBasicInfo(requests);
            return R.ok(result);
        } catch (Web3ApiException e) {
            return R.fail(e.getHttpStatus(), "API错误: " + e.getMessage());
        } catch (Exception e) {
            return R.fail("获取代币基础信息失败: " + e.getMessage());
        }
    }

    /**
     * 获取代币交易信息
     */
    @Operation(summary = "获取代币交易信息", description = "获取代币的交易信息，包括价格、交易量、交易数据、供应量、持有人数、流动性等")
    @PostMapping("/price-info")
    @SaIgnore
    public R<JSONObject> getTokenPriceInfo(@RequestBody List<TokenPriceInfoRequest> requests) {
        try {
            JSONObject result = web3TokenService.getTokenPriceInfo(requests);
            return R.ok(result);
        } catch (Web3ApiException e) {
            return R.fail(e.getHttpStatus(), "API错误: " + e.getMessage());
        } catch (Exception e) {
            return R.fail("获取代币交易信息失败: " + e.getMessage());
        }
    }

    /**
     * 获取代币榜单
     */
    @Operation(summary = "获取代币榜单", description = "根据价格波动、交易量或市值获取代币排名列表")
    @GetMapping("/toplist")
    @SaIgnore
    public R<JSONObject> getTokenTopList(TokenTopListQuery query) {
        try {
            JSONObject result = web3TokenService.getTokenTopList(query);
            return R.ok(result);
        } catch (Web3ApiException e) {
            return R.fail(e.getHttpStatus(), "API错误: " + e.getMessage());
        } catch (Exception e) {
            return R.fail("获取代币榜单失败: " + e.getMessage());
        }
    }

    /**
     * 获取代币持有人信息
     */
    @Operation(summary = "获取代币持有人信息", description = "获取前20名持有者地址及对应代币持仓数量")
    @GetMapping("/holder")
    @SaIgnore
    public R<JSONObject> getTokenHolders(TokenHolderQuery query) {
        try {
            JSONObject result = web3TokenService.getTokenHolders(query);
            return R.ok(result);
        } catch (Web3ApiException e) {
            return R.fail(e.getHttpStatus(), "API错误: " + e.getMessage());
        } catch (Exception e) {
            return R.fail("获取代币持有人信息失败: " + e.getMessage());
        }
    }
}